import { toSuccess, useEntityManager } from "@sker/core"
import { KsAccount } from "@sker/entities"
import { useData } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({
        userId: z.string(),
        has_live: z.boolean(),
        cityName: z.optional(z.string()),
        sex: z.optional(z.string()),
        avatar: z.optional(z.string()),
        description: z.optional(z.string()),
    })
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const one = await m.findOne(KsAccount, { where: { user_id: body.userId } })
        if (!one) throw new Error(`参数错误`)
        console.log(body)
        const data = m.create(KsAccount, {
            id: one?.id,
            has_live: body.has_live || one.has_live,
            city: body.cityName || one.city,
            sex: body.sex || one.sex,
            avatar: body.avatar || one.avatar,
            description: body.description || one.description,
            update_date: new Date()
        });
        await m.save(KsAccount, data)
        return toSuccess(true)
    })
}